﻿Imports System.Globalization
Imports System.Data.OleDb
Public Class Profile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Enable or disable edit profile button , if member is the profile owner or not.
        If Request.Params("Tourist") = User.Identity.Name Then
            btn_EditProfile.Visible = True
            btn_DeleteGuide.Visible = True
        Else
            btn_EditProfile.Visible = False
            btn_DeleteGuide.Visible = False
        End If
        If Request.Params("Tourist") = "" Then
            Response.Redirect("~/Account/Login.aspx")
        End If
        'Set the name and the role of the member.
        lbl_UserName.Text = Request.Params("Tourist")
        lbl_RoleName.Text = String.Join("", System.Web.Security.Roles.GetRolesForUser(Request.Params("Tourist")))
        'Populate the messages
        Comments.GetMessages(Request.Params("Tourist"), CommentsLtr)

        'Check if owner of profile has any favorite beach
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        Dim SqlString As String = "SELECT * FROM FavoriteBeach_WithName WHERE Tourist = @f1"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        oleDbConn.Open()
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", Request.Params("Tourist"))

        'Getting records (only 1 line for selected tourist)
        Dim dr As OleDbDataReader = cmd.ExecuteReader()
        If dr.HasRows Then
            dr.Read()
            If Not IsDBNull(dr("Beach_FK")) Then
                lbl_NoFavBeach.Visible = False
                lbl_FavBeach.Text = dr("BeachName")
                lbl_FavBeach.Visible = True
                rpt_FavBeach.Visible = True
            End If
        End If

        'Check if owner of profile has added a sand castle
        Dim SandCastleCountSql As String = "SELECT * FROM SandCastle WHERE Tourist = @f1"
        Dim cmdCastleCount As OleDbCommand = New OleDbCommand(SandCastleCountSql, oleDbConn)
        cmdCastleCount.CommandType = CommandType.Text
        cmdCastleCount.Parameters.AddWithValue("@f1", Request.Params("Tourist"))

        Dim drCastleCount As OleDbDataReader = cmdCastleCount.ExecuteReader()
        If drCastleCount.HasRows = True Then
            drCastleCount.Read()
            If Not IsDBNull(drCastleCount("CastleName")) Then
                lbl_NoSandCastle.Visible = False
                lbl_SandCastle.Text = drCastleCount("CastleName")
                lbl_SandCastle.Visible = True
                rpt_SandCastle.Visible = True
            End If
        End If

        'Checking if owner of profile is participating in any event.
        Dim SqlStringEvents As String = "SELECT * FROM EventNameWithAttendees WHERE Attendee = @f1"
        Dim cmdEvents As OleDbCommand = New OleDbCommand(SqlStringEvents, oleDbConn)
        cmdEvents.CommandType = CommandType.Text
        cmdEvents.Parameters.AddWithValue("@f1", Request.Params("Tourist"))

        'Getting records (only 1 line for selected tourist)
        Dim drEvents As OleDbDataReader = cmdEvents.ExecuteReader()
        If drEvents.HasRows Then
            lbl_Events.Visible = False
        Else
            lbl_Events.Visible = True
        End If

        'Checking if owner of profile has visited a place in greece
        Dim RegionSql As String = "SELECT * FROM Profile_Region WHERE Tourist = @f1"
        Dim cmdRegion As OleDbCommand = New OleDbCommand(RegionSql, oleDbConn)
        cmdRegion.CommandType = CommandType.Text

        cmdRegion.Parameters.AddWithValue("@f1", Request.Params("Tourist"))
        Dim drRegion As OleDbDataReader = cmdRegion.ExecuteReader()
        Dim lbl As Label = AccordionPaneAboutMe.FindControl("lbl_Places")
        If drRegion.HasRows = True Then
            lbl.Visible = False
        Else
            lbl.Visible = True
        End If
        'Check if owner of profile has albums in order to display or not the lbl_Albums.
        Dim AlbumsCountSql As String = "SELECT * FROM Albums WHERE Tourist = @f1"
        Dim cmdAlbumsCount As OleDbCommand = New OleDbCommand(AlbumsCountSql, oleDbConn)
        cmdAlbumsCount.CommandType = CommandType.Text
        cmdAlbumsCount.Parameters.AddWithValue("@f1", Request.Params("Tourist"))
        Dim drAlbumsCount As OleDbDataReader = cmdAlbumsCount.ExecuteReader()
        If drAlbumsCount.HasRows = True Then
            lbl_Albums.Visible = False
        Else
            lbl_Albums.Visible = True
        End If
        'Check if owner of profile has added any beaches to his guide to enable or disable the delete button.
        Dim hasGuideSql As String = "SELECT * FROM Guides WHERE GuideCreator = @f1"
        Dim cmdhasGuide As OleDbCommand = New OleDbCommand(hasGuideSql, oleDbConn)
        cmdhasGuide.CommandType = CommandType.Text
        cmdhasGuide.Parameters.AddWithValue("@f1", Request.Params("Tourist"))
        Dim drhasGuide As OleDbDataReader = cmdhasGuide.ExecuteReader()
        If drhasGuide.HasRows = True Then
            btn_DeleteGuide.Enabled = True
        Else
            btn_DeleteGuide.Enabled = False
        End If
        If IsPostBack = False Then

            'Not counting activity if a tourist is visiting his own profile
            If User.Identity.Name <> Request.Params("Tourist") Then
                'Check if tourist has already viewed this profile
                Dim sqlCheckUser As String = "SELECT COUNT(*) FROM Profiles_Viewed WHERE TouristVisitor = @f1 AND ProfileVisited = @f2"
                Dim cmdCheck As OleDbCommand = New OleDbCommand(sqlCheckUser, oleDbConn)
                cmdCheck.CommandType = CommandType.Text
                cmdCheck.Parameters.AddWithValue("@f1", User.Identity.Name)
                cmdCheck.Parameters.AddWithValue("@f2", Request.Params("Tourist"))
                Dim userCount As Integer = cmdCheck.ExecuteScalar()
                If (userCount = 0) Then
                    'Counting the activity that the user has viewed this particular profile since user has not visited the profile
                    Dim ProfilesViewedSql As String = "INSERT INTO Profiles_Viewed (TouristVisitor, ProfileVisited) VALUES (@f1, @f2)"
                    Dim cmdViewed As OleDbCommand = New OleDbCommand(ProfilesViewedSql, oleDbConn)
                    cmdViewed.CommandType = CommandType.Text
                    cmdViewed.Parameters.AddWithValue("@f1", User.Identity.Name)
                    cmdViewed.Parameters.AddWithValue("@f2", Request.Params("Tourist"))
                    cmdViewed.ExecuteNonQuery()
                End If
            End If

            'Count Activity for page
            Dim SqlStringActivity As String = "INSERT INTO SiteActivity(Activity) VALUES (@f1)"
            Dim cmdActivity As OleDbCommand = New OleDbCommand(SqlStringActivity, oleDbConn)
            cmdActivity.CommandType = CommandType.Text
            cmdActivity.Parameters.AddWithValue("@f1", "Members")
            cmdActivity.ExecuteNonQuery()

        End If
        oleDbConn.Close()
    End Sub

    Protected Function getDateFrom(ByVal dateStr As Date) As String
        Dim retDate As String = dateStr.ToString("dd MMMM yyyy", CultureInfo.CreateSpecificCulture("en-US"))
        Return retDate
    End Function

    Protected Function getAgeFrom(ByVal dateStr As Date) As String
        Dim retDate As Integer = Convert.ToInt32(dateStr.ToString("yyyy", CultureInfo.CreateSpecificCulture("en-US")))
        Dim currentDate As Integer = DateTime.Now.Year
        Dim age As Integer = currentDate - retDate
        Return CStr(age) & " years old"
    End Function

    Protected Sub btn_Comment_Click(sender As Object, e As EventArgs) Handles btn_Comment.Click
        Dim btn As Button = DirectCast(sender, Button)
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        Dim SqlString As String = "INSERT INTO [Personal_Messages] ([CommentsName], [Title], [TouristName], [CommentsBody], [DateAdded]) VALUES (@f1, @f2, @f3, @f4, Now())"
        oleDbConn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f2", tb_Title.Text)
        cmd.Parameters.AddWithValue("@f3", Request.Params("Tourist"))
        cmd.Parameters.AddWithValue("@f4", tb_Comment.Text)
        cmd.ExecuteNonQuery()
        oleDbConn.Close()
        oleDbConn.Dispose()
        cmd.Dispose()
        Comments.GetMessages(Request.Params("Tourist"), CommentsLtr)
        tb_Comment.Text = ""
        tb_Title.Text = ""
    End Sub

    Protected Function getTouristName() As String
        Return Request.Params("Tourist")
    End Function

    Protected Function getTotalPosts(ByVal totalPosts As Integer) As String
        'If user has made 20 posts then he is upgraded to Greece Lover
        Dim userName = Request.Params("Tourist")
        If totalPosts > 19 Then
            'Adding user to roles as Tourist if it is not a Tourist already
            If (Roles.IsUserInRole(userName, "Swimmer")) Then
                Roles.RemoveUserFromRole(userName, "Swimmer")
                Roles.AddUserToRole(userName, "Tourist")
                lbl_RoleName.Text = String.Join("", System.Web.Security.Roles.GetRolesForUser(Request.Params("Tourist")))
            End If
        End If
        If totalPosts > 39 Then
            'Adding user to roles as Greece Lover if he is not Greece Lover already
            If (Roles.IsUserInRole(userName, "Tourist")) Then
                Roles.RemoveUserFromRole(userName, "Tourist")
                Roles.AddUserToRole(userName, "Greece Lover")
                lbl_RoleName.Text = String.Join("", System.Web.Security.Roles.GetRolesForUser(Request.Params("Tourist")))
            End If
        End If
        Return totalPosts.ToString
    End Function

    Protected Sub btn_DeleteGuide_Click(sender As Object, e As EventArgs) Handles btn_DeleteGuide.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        Dim SqlString As String = "DELETE FROM Guides WHERE GuideCreator = @f1"
        oleDbConn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.ExecuteNonQuery()
        btn_DeleteGuide.Enabled = False
        oleDbConn.Close()
        oleDbConn.Dispose()
    End Sub

    Protected Sub btn_Guide_Click(sender As Object, e As EventArgs) Handles btn_Guide.Click
        Response.Redirect("ViewGuide.aspx?GuideCreator=" + Request.Params("Tourist"))
    End Sub
End Class